/*
     MUNILIST   Listing magnitude and time from FITS, MAT files.
     Copyright (C) 1997  Filip Hroch, Masaryk University, Brno, CZ

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

     E-mail: hroch@physics.muni.cz
     Web:    http://www.sci.muni.cz/~hroch/index.html
     Postal address: F.Hroch
                     Department of Theoretical Physics and Astrophysics
                     Masaryk University
                     Kotlarska 2
                     611 37 Brno
                     Czech Republic

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C                                                                      C
C !!!!!!!!!!!!!!!!     This is UNIX version  !!!!!!!!!!!!!!!!!!!!!!!!! C
C                                                                      C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define LEN 80

main(argc,argv)
int argc;
char *argv[];
{
FILE *fopen(),*fp;
int i,l,column = 4,nstars = 0,stars[200],icit = 1,outlst = 0;
double min0,per = -1.0,delta,alpha = -1.0;
char namfits[LEN]="",namdir[LEN]="",line[LEN],sufix[LEN]="";
char *j;
int stdinput = 0;

fprintf(stderr,"MUNILIST Version 1.0, Copyright (C) 1997 F.Hroch, Masaryk University, Brno, CZ\n\r");
fprintf(stderr,"Munilist comes with ABSOLUTELY NO WARRANTY; This is free software, and you are\n\r");
fprintf(stderr,"welcome to redistribute under certain conditions; see 'gnulic.doc' for details.\n\r\n\r");

for( i = 0; i < argc; i++) {
  if( (j = strstr(argv[i],"@")) != NULL ) {         /* dir file */
     sprintf(namdir,"%s",j + 1);
     stdinput = ! strlen(namdir); }
  if( (j = strstr(argv[i],"col=")) != NULL )        /* column */
    if( sscanf(j + 4,"%d",&l) == 1 ) column = l; 
  if( (j = strstr(argv[i],"suf=")) != NULL )        /*  sufix */
     sprintf(sufix,"%s",j + 4);
  if( sscanf(argv[i],"%d",&l) != 0 )                /* star */
    stars[nstars++] = l; 
  if( strstr(argv[i],"-of") != NULL ) outlst = 1;
  if( (j = strstr(argv[i],"ele=")) != NULL )        /* elements */
    if( sscanf(j + 4,"%lf%*c%lf",&min0,&per) != 2 ) 
      per = -1.0; 
  if( (j = strstr(argv[i],"coo=")) != NULL )        /* alpha,delta */
    if( sscanf(j + 4,"%lf%*c%lf",&alpha,&delta) != 2 ) 
      alpha = -1.0; 
}

if( nstars == 0 && argc >1 ) {
  fprintf(stderr,"No input stars.\n\r");
  exit(0); }

if( strlen(namdir) ) {            /* image names from file */
  if( (fp = fopen(namdir,"r")) == NULL ) {
    fprintf(stderr,"Can't open file %s.\n\r",namdir);
    exit(0); }
  while( fscanf(fp,"%s",namfits) != EOF ) {
    /*fprintf(stderr,"%s:\n",namfits);*/
    if( munilist_(namfits,&column,&nstars,stars,&outlst,
                  &min0,&per,&alpha,&delta,sufix) != 0)
      fprintf(stderr,"Can't read the data file: %s\n\r",namfits);
    icit++; }
  fclose(fp); }

for( i = 1; i < argc; i++)         /* image names from command line */
  if( strstr(argv[i],"=") == NULL && strstr(argv[i],"@") == NULL && 
      sscanf(argv[i],"%d",&l) == 0 && strstr(argv[i],"-") == NULL ) {
    strcpy(namfits,argv[i]);
    /*fprintf(stderr,"%s:\n",namfits);*/
    if( munilist_(namfits,&column,&nstars,stars,&outlst,
                  &min0,&per,&alpha,&delta,sufix) != 0)
      fprintf(stderr,"Can't read the data file: %s\n\r",namfits);
    icit++; }

if( stdinput )               /* images names from standart input */
  while( gets(namfits) != NULL ) {
    /*fprintf(stderr,"%s:\n",namfits);*/
    if( munilist_(namfits,&column,&nstars,stars,&outlst,
                  &min0,&per,&alpha,&delta,sufix) != 0)
      fprintf(stderr,"Can't read the data file: %s\n\r",namfits);
    icit++; }

if( argc == 1 && icit == 1) {                         
  fprintf(stderr,"MUNILIST   Listing magnitude and time from FITS, MAT files.\n\r\n\r");
  fprintf(stderr,"Usage:munilist [@dirfile] [col=column] [-of] [line_var] [line_comp1] ..\n\r"); 
  fprintf(stderr,"               [ele=min0,period] [suf=suffix] [image1]..\n\r\n\r");
  fprintf(stderr,"@dirfile= file with name(s) of stars catalogue(s)\n\r");
  fprintf(stderr,"          (for only '@', read catalogue(s) name(s) from standart input)\n\r");
  fprintf(stderr,"column= column for listing (4 = Aperture 1, default)\n\r");
  fprintf(stderr,"-of= full output list (default short output list)\n\r");
  fprintf(stderr,"coo= alpha,delta (degrees) of variable  stars for heliocentric correction\n\r");
  fprintf(stderr,"ele= epoch, period of variable stars for phase output\n\r");
  fprintf(stderr,"suf= suffix of listing files (default = 'MAT')\n\r");
  fprintf(stderr,"line_var, line_comp1...= line with stars\n\r");
  fprintf(stderr,"image1...= name(s) of simple image(s)\n\r\n\r");
}

}

